package com.hxzy.vo.system;

import com.alibaba.fastjson.annotation.JSONField;
import com.hxzy.entity.system.SysUser;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义认证用户类
 */
@Getter
@Setter
public class HxzyUser  implements UserDetails {

    private SysUser sysUser;

    //令牌
    private String token;

    //令牌开始时间
    private Long beginDate;
    //令牌过期时间
    private Long endDate;



    /**
     * 是不是超级管理员
     * @return
     */
    public boolean isSuperMan(){
        if(this.roles!=null && this.roles.contains("admin")){
            return true;
        }
        return false;
    }

    /**
     * 角色权限字符
     */
    private Set<String> roles;

    /**
     * 菜单权限字符串
     */
    private Set<String> permission;


    public HxzyUser() {
    }

    /**
     * 权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> sets=new HashSet<>();
        //角色
        if(this.roles!=null && this.roles.size()>0){
            for(String s: this.roles) {
                sets.add(new SimpleGrantedAuthority("ROLE_"+s));
            }
        }

        //菜单
        if(this.permission!=null && this.permission.size()>0){
            for(String s: this.permission) {
                sets.add(new SimpleGrantedAuthority(s));
            }
        }

        return sets;
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return this.sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return this.sysUser.getName();
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }
}
